#include "user.h"
#include <QDebug>
#include <QCursor>

user::user(QObject *parent)
    : QObject{parent}
{
    // 对100组过滤数据进行初始化
    int dataCount = 100;
    this->m_startID1.resize(dataCount);
    this->m_startID1.fill("", dataCount);
    this->m_endID1.resize(dataCount);
    this->m_endID1.fill("", dataCount);
    this->m_enableFlag1.resize(dataCount);
    this->m_enableFlag1.fill(0, dataCount);
    this->m_startID2.resize(dataCount);
    this->m_startID2.fill("", dataCount);
    this->m_endID2.resize(dataCount);
    this->m_endID2.fill("", dataCount);
    this->m_enableFlag2.resize(dataCount);
    this->m_enableFlag2.fill(0, dataCount);

    mSerial = new serial();
    mSerialEvent = new SerialEvent();

    mSerial->serialScan();

    connect(this->mSerial, &serial::readDataCompleted, this, [=](){
        uchar can = (uchar)this->mSerial->getSerialData().at(1);
        uchar mode = (uchar)this->mSerial->getSerialData().at(2);
        uchar index = (uchar)this->mSerial->getSerialData().at(3);
        if((mode & 0x01) == 0x01) {
            setOneResistanceFlag(can, (mode & 0x10) ? 0 : 1);
            setOneStartID(can, index, QString("%1%2%3%4")
                          .arg((uchar)this->mSerial->getSerialData().at(7),0,16)
                          .arg((uchar)this->mSerial->getSerialData().at(6),0,16)
                          .arg((uchar)this->mSerial->getSerialData().at(5),0,16)
                          .arg((uchar)this->mSerial->getSerialData().at(4),0,16));
            setOneEndID(can, index, QString("%1%2%3%4")
                          .arg((uchar)this->mSerial->getSerialData().at(11),0,16)
                          .arg((uchar)this->mSerial->getSerialData().at(10),0,16)
                          .arg((uchar)this->mSerial->getSerialData().at(9),0,16)
                          .arg((uchar)this->mSerial->getSerialData().at(8),0,16));
            setOneEnableFlag(can, index, (uchar)this->mSerial->getSerialData().at(12));
        }
        emit configProgressChanged(index + 1, 100);
//        qDebug() << this->mSerial->getSerialData();
        if(index < 100 - 1) {
            sendSerialData((mode & 0x01) == 0x01 ? "read" : "write", can, ((mode & 0x10) ? true : false), index + 1);
        } else {
            if((mode & 0x01) == 0x01) {
                emit filterIDChanged(index);
            }
        }
    });

//    createFilterSetting("write",1,1);
}

user::~user()
{

}

serial* user::getSerialClass()
{
    return this->mSerial;
}

SerialEvent* user::getSerialEventClass()
{
    return this->mSerialEvent;
}

void user::setOneStartID(int can, int index, QString data)
{
    if(can == 1) {
        this->m_startID1.replace(index, data);
    } else if(can == 2) {
        this->m_startID2.replace(index, data);
    } else {
        qDebug() << "can port error";
    }
}

QString user::getOneStartID(int can, int index)
{
    QString temp = NULL;
    if(can == 1) {
        temp = this->m_startID1.at(index);
    } else if(can == 2) {
        temp = this->m_startID2.at(index);
    } else {
        qDebug() << "can port error";
    }
    return temp;
}

void user::setOneEndID(int can, int index, QString data)
{
    if(can == 1) {
        this->m_endID1.replace(index, data);
    } else if(can == 2) {
        this->m_endID2.replace(index, data);
    } else {
        qDebug() << "can port error";
    }
}

QString user::getOneEndID(int can, int index)
{
    QString temp = NULL;
    if(can == 1) {
        temp = this->m_endID1.at(index);
    } else if(can == 2) {
        temp = this->m_endID2.at(index);
    } else {
        qDebug() << "can port error";
    }
    return temp;
}

void user::setOneEnableFlag(int can, int index, int flag)
{
    if(can == 1) {
        this->m_enableFlag1.replace(index, flag);
    } else if(can == 2) {
        this->m_enableFlag2.replace(index, flag);
    } else {
        qDebug() << "can port error";
    }
}

int user::getOneEnableFlag(int can, int index)
{
    int temp = 0;
    if(can == 1) {
        temp = this->m_enableFlag1.at(index);
    } else if(can == 2) {
        temp = this->m_enableFlag2.at(index);
    } else {
        qDebug() << "can port error";
    }
    return temp;
}

void user::setOneResistanceFlag(int can, int flag)
{
    if(can > 2 || can < 1) {
        qDebug() << "can port error";
        return ;
    }
    this->resistanceFlag[can - 1] = flag;
}

int user::getOneResistanceFlag(int can)
{
    if(can > 2 || can < 1) {
        qDebug() << "can port error";
        return 0;
    }
    return this->resistanceFlag[can - 1];
}

void user::setMyProperty(QString property, QVariant value, int index)
{
    if(property == "startID1") {
        setOneStartID(1, index, value.toString());
    } else if(property == "endID1") {
        setOneEndID(1, index, value.toString());
    } else if(property == "enFlag1") {
        setOneEnableFlag(1, index, value.toInt());
    } else if(property == "startID2") {
        setOneStartID(2, index, value.toString());
    } else if(property == "endID2") {
        setOneEndID(2, index, value.toString());
    } else if(property == "enFlag2") {
        setOneEnableFlag(2,index, value.toInt());
    } else if(property == "rFlag") {
        setOneResistanceFlag(index, value.toInt());
    }
}

QVariant user::getMyProperty(QString property, int index)
{
    QVariant propertyTemp = NULL;
    if(property == "serialCOM") {
        propertyTemp = mSerial->getSerialPort();
    } else if(property == "serialDes") {
        propertyTemp = mSerial->getSerialDescription().at(index);
    } else if(property == "startIDList1") {
        propertyTemp.setValue(this->m_startID1);
    } else if(property == "endIDList1") {
        propertyTemp.setValue(this->m_endID1);
    } else if(property == "enFlagList1") {
        propertyTemp.setValue(this->m_enableFlag1);
    } else if(property == "startIDList2") {
        propertyTemp.setValue(this->m_startID2);
    } else if(property == "endIDList2") {
        propertyTemp.setValue(this->m_endID2);
    } else if(property == "enFlagList2") {
        propertyTemp.setValue(this->m_enableFlag2);
    } else if(property == "rFlag") {
        propertyTemp = getOneResistanceFlag(index);
    } else if(property == "serialState") {
        propertyTemp = this->mSerial->getSerialOnOff();
    }

    return propertyTemp;
}

void user::beginSerialScan()
{
    this->mSerial->serialScan();
}

void user::openSerialPort(QString port)
{
    this->mSerial->serialOpen(port, 115200);
}

void user::closeSerialPort()
{
    this->mSerial->serialClose();
}

void user::sendSerialData(QString mode, int can, bool resistance, int index)
{
    if(can == 0) {
        qDebug() << "CAN port error";
    }
    if(mode.contains("write")) {
        this->mSerial->sendSerialData(createFilterSetting("write", can, resistance, index));
    } else if(mode.contains("read")) {
        this->mSerial->sendSerialData(createFilterSetting("read", can, resistance, index));
    }
}

QByteArray user::createFilterSetting(QString mode, int can, bool resistance, int index)
{
    QByteArray byteArr;
    bool ok = false;
    int dec = 0;
    uchar modeTemp = mode.contains("write") ? 0x2 : 0x1;
    if(resistance) modeTemp |= 0x10;

    byteArr.append((uchar)0x55); // 帧头
    byteArr.append((uchar)can); // CAN端口 0x1 0x2
    byteArr.append(modeTemp); // 0x1读取 0x2写入 0x12写入并使能终端电阻
    byteArr.append((uchar)index); // 过滤组ID
    // 起始ID
    dec = (getOneStartID(can, index).isEmpty() ? "0" : getOneStartID(can, index)).toInt(&ok, 16);
    if(!ok) {
        qDebug() << "string to hex error";
        byteArr.clear();
        return byteArr;
    }
    byteArr.append((uchar)dec);
    byteArr.append((uchar)(dec >> 8));
    byteArr.append((uchar)(dec >> 16));
    byteArr.append((uchar)(dec >> 24));
    // 结束ID
    dec = (getOneEndID(can, index).isEmpty() ? "0" : getOneEndID(can, index)).toInt(&ok, 16);
    if(!ok) {
        qDebug() << "string to hex error";
        byteArr.clear();
        return byteArr;
    }
    byteArr.append((uchar)dec);
    byteArr.append((uchar)(dec >> 8));
    byteArr.append((uchar)(dec >> 16));
    byteArr.append((uchar)(dec >> 24));
    byteArr.append((uchar)getOneEnableFlag(can, index)); // 1使能/0失能
    byteArr.append((uchar)0xAA); // 帧尾

//    qDebug() << byteArr;
    return byteArr;
}

int user::hexToDec(QString hex)
{
    bool ok = false;
    int dec = 0;
    dec = hex.toInt(&ok, 16);
    if(!ok) {
        qDebug() << "hex to dec error";
        return -1;
    }

    return dec;
}

QPointF user::getCursorPos()
{
    return QCursor::pos();
}
